class AggregationsController < ApplicationController

    require 'cgi'
    
    before_filter :login_required, :except => [:discover, :all, :all_photos, :photos, :read_photos, :marked_photos, :marked, :show, :show_marked, :bookmarks]
    before_filter :setup_paging, :only => [:show, :all, :marked, :read, :discover, :photos, :all_photos, :read_photos, :marked_photos, :bookmarks, :show_marked]
    layout "default"

    def initialize
        @per_page = 50
    end
   
    # GET /aggregations
    # GET /aggregations.xml
    def index
        #TODO let any user view all aggregations for a given user
        @page_title = 'My Aggregations'
        respond_to do |format|
            format.html {   @aggregations = Aggregation.find_all_by_user_id(current_user.id, :order => 'title ASC')
            render(:layout => 'reader') }
            format.opml {   @aggregations = Aggregation.find_all_by_user_id(current_user.id, :include =>:feeds, :order => 'feeds.title ASC')
            render(:layout => false) }
            format.xml  {   @aggregations = Aggregation.find_all_by_user_id(current_user.id, :order => 'title ASC')
            render :xml => @aggregations }
        end
    end

    def discover
        @page_title = 'Latest Aggregations'
        @aggregations = Aggregation.find(:all, :order => 'aggregations.created_at ASC', :limit => @per_page, :include =>:feeds, :offset => (@per_page * @page))
        respond_to do |format|
            format.html 
            format.xml  { render :xml => @aggregations }
        end
    end

    def manage
        @page_title = 'Manage'
        @user = current_user
        @aggregations = Aggregation.find_all_by_user_id(@user.id, :order => 'title ASC')
        respond_to do |format|
            format.html
            format.xml  { render :xml => @aggregations }
        end
    end

    # GET /aggregations/1
    # GET /aggregations/1.xml
    def show
        @aggregation = Aggregation.find(params[:id])
        user_id = -1
        user_id = current_user.id unless current_user.nil?
        
        @rss_title = @aggregation.title
        @rss_description = @aggregation.description
        
        @entries = Entry.get_entries_for_aggregation(@aggregation.id, user_id, :limit => @per_page, :offset => (@per_page * @page))
        @page_title = @aggregation.title
        respond_to do |format|
            format.html { render(:layout => false) }
            format.opml { render(:layout => false) }
            format.xml  { render :xml => @entries }
            format.rss { render(:template => 'aggregations/show', :layout => false) }
        end
    end

    # GET /aggregations/1
    # GET /aggregations/1.xml
    def show_marked
        
        @user = User.find(params[:user_id])
        @aggregation = Aggregation.find(params[:id])
        
        @rss_title = @aggregation.title
        @rss_description = 'View entries from all entries marked by ' + @user.login + ' in the aggregation ' + @aggregation.title
        @entries = Entry.get_marked_entries_for_aggregation_and_user(@aggregation.id, @user.id, :limit => @per_page, :offset => (@per_page * @page))
        @page_title = 'Entries marked for ' + @aggregation.title
        respond_to do |format|
            format.html { render(:template => 'aggregations/show', :layout => false) }
            format.xml  { render :xml => @entries }
            format.rss { render(:template => 'aggregations/show', :layout => false) }
        end
    end
    
    def all
        @page_title = 'View entries from all aggregations'
        
        @aggregation = Aggregation.new
        @aggregation.title = @page_title
        
        @rss_title = @aggregation.title
        @rss_description = 'View entries from all aggregations'
        
        @entries = Entry.get_entries_for_user(params[:user_id], :limit => @per_page, :offset => (@per_page * @page))        
        respond_to do |format|
            format.html { render(:template => 'aggregations/show', :layout => false) }
            format.xml  { render :xml => @entries }
            format.rss { render(:template => 'aggregations/show', :layout => false) }
        end
    end

    def all_photos
        @page_title = 'View photos from all aggregations'
        @aggregation = Aggregation.new
        @aggregation.title = @page_title
        @photos = EntryImage.get_photos_for_user(params[:user_id], :limit => @per_page, :offset => (@per_page * @page))        
        respond_to do |format|
            format.html { render(:template => 'aggregations/photos', :layout => false) }
            format.xml  { render :xml => @photos }
        end
    end
    
    def photos
        @aggregation = Aggregation.find(params[:id])
        @page_title = "View photos from #{@aggregation.title}"
        @photos = EntryImage.get_entry_images_for_aggregation(@aggregation.id, :limit => @per_page, :offset => (@per_page * @page))       
        respond_to do |format|
            format.html { render(:template => 'aggregations/photos', :layout => false) }
            format.xml  { render :xml => @photos }
        end
    end  
    
    def bookmarks
        @aggregation = Aggregation.find(params[:id])
        @page_title = "View bookmarks from #{@aggregation.title}"
        @entries = Entry.get_bookmarks_for_aggregation(@aggregation.id, -1, :limit => @per_page, :offset => (@per_page * @page))       
        respond_to do |format|
            format.html { render(:template => 'aggregations/bookmarks', :layout => false) }
            format.xml  { render :xml => @entries }
        end
    end
    
    def read_photos
        @page_title = 'View photos from read entries'
        @aggregation = Aggregation.new
        @aggregation.title = @page_title
        @photos = EntryImage.get_read_photos_for_user(params[:user_id], :limit => @per_page, :offset => (@per_page * @page)) | []      
        respond_to do |format|
            format.html { render(:template => 'aggregations/photos', :layout => false) }
            format.xml  { render :xml => @photos }
        end
    end
    
    def marked_photos
        @page_title = 'View photos from marked entries'
        @aggregation = Aggregation.new
        @aggregation.title = @page_title
        @photos = EntryImage.get_marked_photos_for_user(params[:user_id], :limit => @per_page, :offset => (@per_page * @page)) | []
        respond_to do |format|
            format.html { render(:template => 'aggregations/photos', :layout => false) }
            format.xml  { render :xml => @photos }
        end    
    end
    
    def marked
        @page_title = 'View entries that I have marked'
        @aggregation = Aggregation.new
        @aggregation.title = @page_title
        @user = User.find(params[:user_id])
        
        @rss_title = @aggregation.title
        @rss_description = 'View entries from all entries marked by ' + @user.login
        
        @entries = Entry.get_marked_entries_for_user(@user.id, :limit => @per_page, :offset => (@per_page * @page))
        respond_to do |format|
            format.html { render(:template => 'aggregations/show', :layout => false) }
            format.xml  { render :xml => @entries }
            format.rss { render(:template => 'aggregations/show', :layout => false) }
        end
    end

    def read
        @page_title = 'View entries I have read'
        @aggregation = Aggregation.new
        @aggregation.title = @page_title
        @entries = Entry.get_entries_read_for_user(current_user.id, :limit => @per_page, :offset => (@per_page * @page))
        respond_to do |format|
            format.html { render(:template => 'aggregations/show', :layout => false) }
            format.xml  { render :xml => @entries }
        end
    end

    def duplicate
        @page_title = 'Duplicate Aggregation'
        
        @aggregation = Aggregation.find(params[:id], :include => 'feeds')
        @new_aggregation = Aggregation.new
        
        @new_aggregation.title = @aggregation.title
        @new_aggregation.description = @aggregation.description
        @new_aggregation.user_id = current_user.id
        
        success = false
        if @new_aggregation.save
            @page_title = 'Added aggregation ' + @new_aggregation.title
            @aggregation.feeds.each do |feed|
                @new_aggregation.feeds << feed
            end
            success = true
        end
        
        respond_to do |format|
            if success
                flash[:notice] = "Successfully added the " + @new_aggregation.title + " to your aggregations"
                format.html { redirect_to(CGI::unescape(params[:return_page])) if params[:return_page] }
                format.xml  { render :xml => @new_aggregation, :status => :created, :location => @new_aggregation }
            else
                flash[:notice] = "Some kind of bad thing happened when we tried to add the requested aggregation to your account.  We'll look into it.  If you want press the back button and try again.  If that doesn't work yell at the computer for a bit, it always helps me feel better.  Believe me we are just as upset about all this as you are."
                format.html 
                format.xml  { render :xml => @new_aggregation.errors }
            end
        end
    end
    
    # GET /aggregations/new
    # GET /aggregations/new.xml
    def new
        @page_title = 'New Aggregation'
        @aggregation = Aggregation.new

        respond_to do |format|
            format.html # new.html.erb
            format.xml  { render :xml => @aggregation }
        end
    end

    # GET /aggregations/1/edit
    def edit
        @aggregation = Aggregation.find(params[:id])
        @page_title = 'Editing ' + @aggregation.title
    end

    # POST /aggregations
    # POST /aggregations.xml
    def create
        @aggregation = Aggregation.new(params[:aggregation])
        @aggregation.name = make_safe_uri(@aggregation.title)
        @aggregation.user = current_user

        respond_to do |format|
            if @aggregation.save
                flash[:notice] = @aggregation.title + ' created.  Now add some feeds:'
                format.html { redirect_to(new_aggregation_feed_path(@aggregation)) }
                format.xml  { render :xml => @aggregation, :status => :created, :location => @aggregation }
            else
                format.html { render :action => "new" }
                format.xml  { render :xml => @aggregation.errors }
            end
        end
    end

    # PUT /aggregations/1
    # PUT /aggregations/1.xml
    def update
        @aggregation = Aggregation.find(params[:id])

        respond_to do |format|
            if @aggregation.update_attributes(params[:aggregation])
                flash[:notice] = 'Aggregation was successfully updated.'
                format.html { redirect_to(manage_user_aggregations_path(:user_id => current_user.id)) }
                format.xml  { head :ok }
            else
                format.html { render :action => "edit" }
                format.xml  { render :xml => @aggregation.errors }
            end
        end
    end

    # DELETE /aggregations/1
    # DELETE /aggregations/1.xml
    def destroy
        @aggregation = Aggregation.find(params[:id])
        @aggregation.destroy

        respond_to do |format|
            flash[:notice] = @aggregation.title + ' has been removed.'
            format.html { redirect_to(manage_user_aggregations_url(current_user)) }
            format.xml  { head :ok }
        end
    end

    # associate an existing feed with an aggregation
    def associate_feed
        @aggregation = Aggregation.find(params[:id])
        @feed = Feed.find(params[:feed_id])
        current_feed = @aggregation.feeds.find(@feed) rescue nil

        if current_feed.nil?
            @aggregation.feeds << @feed
            notice =  @feed.title + ' was successfully added to ' + @aggregation.title
        else
            notice =  @feed.title + ' is already a part of ' + @aggregation.title
        end

        respond_to do |format|
            if params[:search_terms]
                redirect_path = url_for({:controller => 'feeds', :action => 'search', :aggregation_id => @aggregation, :id => @feed, :search_terms => params[:search_terms]})
            else
                redirect_path = aggregation_feeds_url(@aggregation)
            end

            if @aggregation.save
                flash[:notice] = notice
                format.html { redirect_to(redirect_path) }
                format.xml  { render :xml => @aggregation, :status => :added_feed, :location => @aggregation }
            else
                flash[:notice] = 'Could not add ' + @feed.title + ' to ' + @aggregation.title
                format.html { redirect_to(redirect_path) }
                format.xml  { render :xml => @aggregation.errors }
            end
        end
    end

    #disassciates a feed from an aggregation
    def remove_feed
        @aggregation = Aggregation.find(params[:id])
        @feed = Feed.find(params[:feed_id])
        @aggregation.feeds.delete(@feed)
        respond_to do |format|
            if @aggregation.save
                flash[:notice] = @feed.title + ' was successfully removed from ' + @aggregation.title
                format.html { redirect_to(aggregation_feeds_path(@aggregation)) }
                format.xml  { render :xml => @aggregation, :status => :removed_feed, :location => @aggregation }
            else
                flash[:notice] = 'Could not remove ' + @feed.title + ' from ' + @aggregation.title
                format.html { redirect_to(aggregation_feeds_path(@aggregation)) }
                format.xml  { render :xml => @aggregation.errors }
            end
        end
    end

    protected
    
    def setup_paging
        @page = params[:page].to_i || 0
        @next_page = (@page + 1).to_s
        @previous_page  = (@page -1).to_s
    end
end
